import {HttpClient} from '@angular/common/http';
import {Component} from '@angular/core';
import {Router} from '@angular/router';
import {NzIconService, NzMessageService} from 'ng-zorro-antd';
import {environment} from '../../environments/environment';
import {AppService} from '../app.service';
import {AppInfoDto, LoginDto} from '../basic/basic.model';
import {CommonTool} from '../common/common.tool';
import {HomeService} from './home.service';
import {BasicService} from '../basic/basic.service';
import {BUTTON_CODE} from '../config/button-code';
import {CommonHttp} from '../common/common.http';
import {HTTP_URLS} from '../config/app-http.url';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss']
})
export class HomeComponent {
  public menus: Array<any>;
  public isCollapsed: boolean;
  public title: string;
  public isPassWordVisible: boolean;
  public setupPasswordVisible: boolean;
  public passwordInfo: {
    password: string
    newPassword: string
    againPassword: string,
  };
  public passwordConfig: {
    password: boolean,
    newPassword: boolean
    againPassword: boolean
    passwordType: string,
  };
  public isUpdateSecret: boolean;
  public url = environment.config.url + '/share/page/';
  public tokenInfo: LoginDto;
  public userInfo: any;
  public BUTTON_CODE = BUTTON_CODE;

  constructor(private router: Router,
              private http: HttpClient,
              public appService: AppService,
              public homeService: HomeService,
              public basicService: BasicService,
              private message: NzMessageService,
              public iconService: NzIconService) {
    this.iconService.fetchFromIconfont({
      scriptUrl: '//at.alicdn.com/t/font_1580957_uw99thxuc6e.js'
    });
    // 获取系统信息
    const info = localStorage.getItem(environment.appKey);
    if (info) {
      const appInfos: Array<AppInfoDto> = JSON.parse(info);
      for (const appInfo of  appInfos) {
        if (appInfo.key === 'title') {
          this.title = appInfo.value;
        }
      }
    } else {
      const commonHttp = new CommonHttp();
      const httpModel = commonHttp.get(HTTP_URLS.getMyAppInfo);
      if (httpModel.isSuccess) {
        const appInfos: Array<AppInfoDto> = JSON.parse(httpModel.data);
        for (const appInfo of  appInfos) {
          if (appInfo.key === 'title') {
            this.title = appInfo.value;
          }
        }
        localStorage.setItem(environment.appKey, JSON.stringify(appInfos));
      }
    }
    // 获取用户token信息
    this.tokenInfo = new LoginDto(JSON.parse(CommonTool.getCookie(environment.tokenKey)));
    // 获取用户菜单
    const menus = localStorage.getItem(environment.menuKey);
    if (menus) {
      this.menus = JSON.parse(menus);
    } else {
      this.menus = [];
    }
    // 获取用户信息
    this.userInfo = {};
    this.homeService.getMyAppUser().subscribe((res) => {
      this.userInfo = res;
      this.homeService.getMyAppUserInfo().subscribe(data => {
        Object.assign(this.userInfo, data ? data : {});
      });
    });
    this.isPassWordVisible = false;
    this.setupPasswordVisible = false;
    this.passwordInfo = {password: '', newPassword: '', againPassword: ''};
    this.passwordConfig = {password: false, newPassword: false, againPassword: false, passwordType: ''};
    this.isUpdateSecret = false;
  }

  /**
   * 打开菜单
   */
  public toggleCollapsed() {
    this.isCollapsed = !this.isCollapsed;
  }

  /**
   * 修改密码关闭
   */
  public modifyPassWordCancel() {
    this.isPassWordVisible = false;
    this.passwordInfo = {password: '', newPassword: '', againPassword: ''};
    this.passwordConfig = {password: false, newPassword: false, againPassword: false, passwordType: ''};
  }

  /**
   * 修改密码提交
   */
  public modifyPassWordOk(data) {
    if (data.newPassword === data.password) {
      this.message.warning('新密码不能和旧密码相同！');
      return;
    }
    this.appService.setLoading(true);
    this.homeService.updateMyAppUserPassword({
      newPassword: data.newPassword,
      password: data.password ? data.password : null
    }).subscribe(() => {
      this.passwordInfo = {
        password: '',
        newPassword: '',
        againPassword: ''
      };
      this.userInfo['passwordExist'] = true;
      this.appService.setLoading(false);
      this.message.success('密码修改成功！');
      this.isPassWordVisible = false;
      this.basicService.logOut();
    });
  }

  /**
   * 退出登录
   */
  public logOut() {
    this.basicService.logOut();
  }

  /**
   * 跳转到个人信息
   */
  public jumpPersonal(url) {
    this.router.navigate([url]);
  }

  /**
   * 复制
   */
  public copy(data, html, doc) {
    const input = document.createElement('input');
    input.value = data;
    document.body.appendChild(input);
    input.select();
    if (document.execCommand('Copy')) {
      doc.style.display = 'none';
      html.style.display = 'inline-block';
    }
    input.style.display = 'none';
  }

  /**
   * 跳转
   */
  public jump(url) {
    if (url) {
      this.router.navigateByUrl(url);
    }
  }
}
